
$(document).ready(function() {
    var parser = get_request("parser", 0);
    var page = new Vue({
        el: "#page-content",
        data: {
            tasks: null,
            modal: {
                title: "",
                comments: "",
            },
            search: {
                task: "",
            },
        },
        methods: {
            addNew: function(event) {
                this.modal.title = "";
                this.modal.comments = "";
                $("#theFile").val("");
                $("#theModal").modal();
            },
            update: function(event) {
                var ok = $(event.currentTarget).attr("ok");
            },
            ensure: function(event) {
                if (this.modal.title == "") {
                    alert("请输入标题.");
                    return;
                }

                if (this.modal.comments == "") {
                    alert("请输入详细描述.");
                    return;
                }

                var file = $("#theFile").get(0).files[0];
                if (typeof(file) == "undefined") {
                    alert("请选择要分析的文件！");
                    return;
                }

                __file_upload("api.v1.task.upload", $("#theFile").get(0), {parser: parser, title: this.modal.title, comments: this.modal.comments}, function(res) {
                    console.debug(res);
                    alert("上传成功!");
                    reload_data();
                });
                $("#theModal").modal("hide");
            },
            searchTask: function(event) {
                var text = this.search.task;
                for (var k in this.tasks) {
                    this.tasks[k].filteredout = false;
                    if (text.length > 0) {
                        if (this.tasks[k].id.indexOf(text) >= 0 ||
                            this.tasks[k].title.indexOf(text) >= 0 ||
                            this.tasks[k].comments.indexOf(text) >= 0 ||
                            this.tasks[k].status_text.indexOf(text) >= 0 ||
                            this.tasks[k].create_time.indexOf(text) >= 0 ||
                            this.tasks[k].result_time.indexOf(text) >= 0
                        ){
                            this.tasks[k].filteredout = false;
                        } else {
                            this.tasks[k].filteredout = true;
                        }
                    }
                }
            },
        }
    });

    var reload_data = function() {
        __request("api.v1.task.list", {parser: parser}, function(res) {
            console.debug(res);
            for (var k in res.data) {
                res.data[k].filteredout = false;
            }
            page.tasks = res.data;
        });
    };
    reload_data();

});

